<?php

namespace catchAdmin\registration\controller;

use catcher\CatchResponse;
use catcher\base\CatchRequest;
use catcher\base\CatchController;
use catchAdmin\registration\service\ReportService;
use catcher\exceptions\FailedException;

class Report extends CatchController
{
    /**
     * 院内随访进展统计-随访进度统计(肿瘤分类、当前任务)
     * @return \think\response\Json
     */
    public function inHospitalFollowTumourType(): \think\response\Json
    {
        $list['tumour_type_patient_num'] = (new ReportService())->tumorTypePatientNum();
        $list['current_in_task_patient'] = (new ReportService())->currentInTaskPatient();
        return CatchResponse::success($list);
    }

    /**
     * 院内随访进展统计-随访进度统计(符合条件的随访患者)
     * @return \think\response\Json
     */
    public function inHospitalFollowEligible(): \think\response\Json
    {
        $where = [
            ['is_in_hospital', 'in', [1, 2]],
            ['tumour_type', '=', '1']
        ];
        $list = (new ReportService())->eligibleShouldFollowPatient($where);
        return CatchResponse::success($list);
    }

    /**
     * 院内随访进展统计-随访进度统计(随访方式分布)
     * @return \think\response\Json
     */
    public function followModeDistributionChart(): \think\response\Json
    {
        $where = [
            ['is_in_hospital', 'in', [1, 2]],
            ['tumour_type', '=', '1']
        ];
        $list = (new ReportService())->followModeDistribution($where);
        return CatchResponse::success($list);
    }

    /**
     * 院内随访进展统计-随访进度统计(无效随访原因统计)
     * @return \think\response\Json
     */
    public function invalidFollowReasonChart(): \think\response\Json
    {
        $where = [
            ['is_in_hospital', 'in', [1, 2]],
            ['tumour_type', '=', '1']
        ];
        $list = (new ReportService())->invalidFollowReason($where);
        return CatchResponse::success($list);
    }

    /**
     * 我的患者分布特征-患者疾病性质分析
     * @return \think\response\Json
     */
    public function patientDiseaseNatureAnalysis(): \think\response\Json
    {
        $list = (new ReportService())->tumorTypePatientNum();
        $ret = [
          'total' => $list[0]['total'],
          'malignantCount' => $list[0]['malignantCount'],
          'benignCount' => $list[0]['benignCount'],
          'dynamicUncertainCount' => $list[0]['dynamicUncertainCount'],
          'unknownCount' => $list[0]['total'] - ($list[0]['malignantCount'] + $list[0]['benignCount'] + $list[0]['dynamicUncertainCount'] + $list[0]['nonTumorCount']),
          'nonTumorCount' => $list[0]['nonTumorCount'],
        ];
        return CatchResponse::success($ret);
    }

    /**
     * 我的患者分布特征-患者恶性肿瘤分布
     * @return \think\response\Json
     */
    public function patientMalignantTumorsDistribution(): \think\response\Json
    {
        $list = (new ReportService())->malignantTumorDistribution();
        return CatchResponse::success($list);
    }

    /**
     * 患者发病特点及影响因素-年龄与性别特征分析
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function ageAndGexderFeatureAnalysis(CatchRequest $request): \think\response\Json
    {
        $type = $request->param("type", 1, "int");

        switch ($type){
            case 1:
                $list = (new ReportService())->genderRatioAnalysis();
                break;
            case 2:
                $list = (new ReportService())->ageAndMeanAnalysis();
                break;
            case 3:
                $list = (new ReportService())->genderAndAgeAnalysis();
                break;
            default:
                throw new FailedException("类型不存在");
        }
        return CatchResponse::success($list);
    }

    /**
     * 患者发病特点及影响因素-病种与地域特征分析
     * @return \think\response\Json
     */
    public function diseaseAndRegionFeatureAnalysis(): \think\response\Json
    {
        $ret = (new ReportService())->diseaseAndRegionAnalysis();
        return CatchResponse::success($ret);
    }

    /**
     *  电话接通率统计
     *  @param CatchRequest $request
     *  @return \think\response\Json
     */
    public function telCompletionRateAnalysis(CatchRequest $request): \think\response\Json
    {
        $type = $request->param('type', 1, 'int');
        if (empty($type)) {
            throw new FailedException('请选择统计类型');
        }
        $ret = (new ReportService())->telCompletionRate($type);
        return CatchResponse::success($ret);
    }

    /**
     * 患者生存情况及影响因素-总体生存情况分析
     * @return \think\response\Json
     */
    public function overallSurvivalAnalysis(): \think\response\Json
    {
        set_time_limit(0);
        ini_set("memory_limit","2G");
        $ret = (new ReportService())->survivalSituationAnalysis();
        return CatchResponse::success($ret);
    }

    /**
     * 患者生存情况及影响因素-分病种生存情况分析
     * @return \think\response\Json
     */
    public function diseaseTypeKmAnalysis(): \think\response\Json
    {
        set_time_limit(0);
        ini_set("memory_limit","2G");
        $ret = (new ReportService())->diseaseTypeAnalysis();
        return CatchResponse::success($ret);
    }

    /**
     * 患者生存情况及影响因素-分科室生存情况分析
     * @return \think\response\Json
     */
    public function deptKmAnalysis(): \think\response\Json
    {
        set_time_limit(0);
        ini_set("memory_limit","2G");
        $ret = (new ReportService())->deptAnalysis();
        return CatchResponse::success($ret);
    }

    /**
     * 患者生存情况及影响因素-首诊医生生存情况分析
     * @return \think\response\Json
     */
    public function doctorKmAnalysis(): \think\response\Json
    {
        set_time_limit(0);
        ini_set("memory_limit","2G");
        $ret = (new ReportService())->doctorAnalysis();
        return CatchResponse::success($ret);
    }

    /**
     * 随访工作量统计
     * @return \think\response\Json
     */
    public function followWorkloadAnalysis(CatchRequest $request): \think\response\Json
    {
        $type = $request->param('type', 1, 'int');
        $ret = (new ReportService())->followWorkload($type);
        return CatchResponse::success($ret);
    }

    /**
     * 随访方式统计-电话
     * @return \think\response\Json
     */
    public function telephoneMethodAnalysis(): \think\response\Json
    {
        set_time_limit(0);
        ini_set("memory_limit","2G");
        return CatchResponse::paginate((new ReportService())->telMethodStatistics());
    }
}